package com.apache.workday.tools;

import java.util.Calendar;

import com.apache.tools.StrUtil;
import com.apache.tools.SystemTools;
import com.apache.workday.Workday;
import com.apache.workday.WorkdayService;
import com.apache.workday.WorkdayUtils;

/**
 * 生成初始化json,显示年中所有的周六日,默认非工作日
 *
 */
public class WorkDayData {

	private static WorkDayData instancet;

	public static WorkDayData getInstance() {
		if (instancet == null) {
			instancet = new WorkDayData();
		}
		return instancet;
	}

	/**
	 * 开始执行实例
	 * @return
	 */
	public void startInstance() {
		startInstance("", "");
	}

	/**
	 * 开始执行实例
	 * @return
	 */
	public void startInstance(String key, String fileName) {
		key = StrUtil.doNull(key, "default");
		fileName = StrUtil.doNull(fileName, "config/workday_config.json");
		String filePath = SystemTools.getInstance().getClassLoaderPath() + fileName;
		WorkdayUtils.getInstance().checkFolder(filePath);
		WorkdayService instance = new WorkdayService(filePath);
		getYearSat(instance);
		getYearSun(instance);
		WorkdayUtils.getInstance().setWorkday(key, instance);
	}

	public void getYearSat(WorkdayService instance) {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int nextyear = calendar.get(Calendar.YEAR) + 1;
		Calendar nowyear = Calendar.getInstance();
		Calendar nexty = Calendar.getInstance();
		nowyear.set(year, 0, 1);
		nexty.set(nextyear, 0, 1);

		calendar.add(Calendar.DAY_OF_MONTH, -calendar.get(Calendar.DAY_OF_WEEK));
		Calendar c = (Calendar) calendar.clone();
		for (; calendar.before(nexty) && calendar.after(nowyear); calendar.add(Calendar.DAY_OF_YEAR, -7)) {
			printf(instance, calendar);
		}
		for (; c.before(nexty) && c.after(nowyear); c.add(Calendar.DAY_OF_YEAR, 7)) {
			printf(instance, c);
		}
	}

	public void getYearSun(WorkdayService instance) {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int nextyear = calendar.get(Calendar.YEAR) + 1;
		Calendar nowyear = Calendar.getInstance();
		Calendar nexty = Calendar.getInstance();
		nowyear.set(year, 0, 1);
		nexty.set(nextyear, 0, 1);

		calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		Calendar c = (Calendar) calendar.clone();
		for (; calendar.before(nexty) && calendar.after(nowyear); calendar.add(Calendar.DAY_OF_YEAR, -7)) {
			printf(instance, calendar);
		}
		for (; c.before(nexty) && c.after(nowyear); c.add(Calendar.DAY_OF_YEAR, 7)) {
			printf(instance, c);
		}
	}

	private void printf(WorkdayService instance, Calendar calendar) {
		int n = 2;
		String year = calendar.get(Calendar.YEAR) + "";
		String month = addZeroForNum(1 + calendar.get(Calendar.MONTH) + "", n);
		String day = addZeroForNum(calendar.get(Calendar.DATE) + "", n);

		String yyyyMMdd = year + "" + month + "" + day;
		Workday dvo = new Workday();
		dvo.setDateStr(yyyyMMdd);
		dvo.setIsWorkday("F");
		instance.setDay(dvo);
	}

	//不足补0
	private String addZeroForNum(String str, int strLength) {
		int strLen = str.length();
		StringBuffer sb = null;
		while (strLen < strLength) {
			sb = new StringBuffer();
			sb.append("0").append(str);// 左(前)补0
			// sb.append(str).append("0");//右(后)补0
			str = sb.toString();
			strLen = str.length();
		}
		return str;
	}

	public static void main(String[] args) {
		WorkDayData.getInstance().startInstance("key1", "config/key2.json");
		System.out.println("it's ok!");
		System.out.println(System.currentTimeMillis());
	}
}
